package com.educate.controller;

import com.educate.common.api.Result;
import com.educate.pojo.Note;
import com.educate.service.NoteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author xiongyu
 * @date 2021/10/28 20:59
 */

@RestController
@RequestMapping("/note")
@Slf4j
public class NoteController {

    @Autowired
    private NoteService noteService;

    /**
     * 增加图书
     * @param note
     * @return
     */
    @PostMapping
    public Result addNote(@RequestBody Note note){
               noteService.addNote(note);
        return Result.success(note.getNoteId());
    }

    /**
     * 删除图书
     * @param noteId
     * @return
     */
    @DeleteMapping("/{noteId}")
    public Result deleteNote(@PathVariable Integer noteId){
        return Result.success(noteService.deleteNote(noteId));
    }

    /**
     * 跟新图书
     * @param note
     * @return
     */

    @PutMapping
    public Result updateNote(@RequestBody Note note){
        return Result.success(noteService.updateNote(note));
    }

    /**
     * 查询图书
     * @param noteId
     * @return
     */
    @GetMapping("/{noteId}")
    public Result queryNotes(@PathVariable Integer noteId){
        return Result.success(noteService.queryNote(noteId));
    }

    /**
     * elasticsearch模糊查询
     * @param title
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/{title}/{pageNum}/{pageSize}")
    public Result queryNotesByTitle(@PathVariable String title, @PathVariable Integer pageNum, @PathVariable Integer pageSize){
        return Result.success(noteService.queryNoteByTitle(title,pageNum,pageSize));
    }

    /**
     * 根据作者id分页查询笔记
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/{pageNum}/{pageSize}")
    public Result queryAllNotes( @PathVariable Integer pageNum, @PathVariable Integer pageSize){
        return Result.success( noteService.queryAllNotes(pageNum,pageSize));
    }
}
